package edu.sctu.boot.admin.generator.utils;

import java.io.*;

public class FileUtils {

    // outputFile为绝对路径，确定所有父路径的文件夹创建完成
    public static void ensurePath(String outputFile) throws IOException {

        // (1) 获得父路径
        String[] tokens = outputFile.split("/");
        int len = tokens.length;

        String[] results = new String[len - 1];
        for (int i = 0; i < len - 1; i++) {
            results[i] = tokens[i];
        }

        String parentPath = String.join("/", results);

        // (2) 父路经文件夹
        File parentDir = new File(parentPath);

        if (!parentDir.exists()) {
            parentDir.mkdirs();
        }

    }

    // 文件中是否包含target字符串
    public static boolean contains(File baseServerFile, String target) throws IOException {

        boolean contains = false;

        StringWriter writer = new StringWriter();

        BufferedReader reader = new BufferedReader(new FileReader(baseServerFile));

        String line = null;

        while ((line = reader.readLine()) != null) {

            writer.write(line);
        }

        return writer.toString().contains(target);
    }
}
